package com.spzx.product.controller;


import com.github.pagehelper.PageHelper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.vo.IndexDataVo;
import com.spzx.product.api.domain.ProductSkuVo;
import com.spzx.product.domain.vo.SkuQuery;
import com.spzx.product.service.IChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "移动App聚合数据")
@RestController
@RequestMapping("/channel")
@Slf4j
public class ChannelController extends BaseController {
    @Autowired
    private IChannelService channelService;
    @Autowired
    private RedisTemplate redisTemplate;

    //    @Operation(summary = "获取商品一级分类")
//    @GetMapping
//    public AjaxResult getLevelOneCategory() {
//        List<CategoryVo> categoryVoList = categoryService.getLevelOneCategory();
//        return success(categoryVoList);
//    }
//    @Operation(summary = "获取畅销商品列表")
//    @GetMapping
//    public AjaxResult getTopSale(){
//        List<ProductSkuVo> productSkuVoList = productSkuService.getTopSale();
//        return success(productSkuVoList);
//    }
    @Operation(summary = "获取首页数据")
    @GetMapping("/index")
    public AjaxResult index() {
        IndexDataVo indexDataVo = channelService.index();
        return success(indexDataVo);
    }

    @Operation(summary = "获取分类嵌套列表")
    @GetMapping("/category")
    public AjaxResult category() {
        return success(channelService.tree());
    }

    @Operation(summary = "显示商品列表")
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public TableDataInfo skuList(@Parameter(description = "当前页码")
                                 @PathVariable Integer pageNum,
                                 @Parameter(description = "每页记录数")
                                 @PathVariable Integer pageSize,
                                 SkuQuery skuQuery) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = channelService.selectProductSkuList(skuQuery);
        return getDataTable(list);
    }

    @Operation(summary = "获取全部品牌")
    @GetMapping("/brand")
    public AjaxResult selectBrandAll() {
        return success(channelService.list());
    }
    @Operation(summary = "商品详情")
    @GetMapping("/item/{skuId}")
    public AjaxResult item(@Parameter(description = "skuId")
                           @PathVariable Long skuId){
        String key = "product:sku:data";
        Boolean flag = redisTemplate.opsForValue().getBit(key, skuId);
        if (!flag) {
            log.error("用户查询商品sku不存在：{}", skuId);
            //查询数据不存在直接返回空对象
            throw new ServiceException("用户查询商品sku不存在");
        }
        return success(channelService.item(skuId));
    }
}
